import numpy as np
import mmap
import time
import multiprocessing
import win32api, win32con
import matplotlib.pyplot as plt


class mythread_ps(multiprocessing.Process):
    def __init__(self, Ydata1, Ydata2, Ydata3, Ydata4, lineEdit_14,calibration_1):
        self.zero = -10
        self.calibration_1 = calibration_1
        self.Ydata1 = Ydata1
        self.Ydata2 = Ydata2
        self.Ydata3 = Ydata3
        self.Ydata4 = Ydata4

        self.chatY = []
        self.chatX = []
        self.chatX1=[]
        for item in range(len(self.Ydata1)):
            # data_0 = calibration_1[0] * float(self.Ydata1[item]) * float(self.Ydata1[item]) + calibration_1[1] * float(
            #     self.Ydata1[item]) + \
            #          calibration_1[2]
            self.chatX1.append(float(self.Ydata2[item])-float(self.Ydata3[item]))
            self.chatY.append(float(self.Ydata1[item]))
            self.chatX.append(item)
        self.lineEdit_14 = lineEdit_14
        self.line1 = None
        self.line2 = None
        self.line3 = None
        self.line4 = None
        self.line5 = None
        self.a1 = None
        self.a2 = None
        self.a3 = None
        self.a4 = None
        fig = plt.figure(figsize=(600, 300))
        self.ax1 = fig.add_subplot(111)
        maxX = 1
        if max(self.chatX1)*1.6 > 1:
            maxX = max(self.chatX1)*1.6
        self.ax1.set_xlim([self.zero, maxX])
        # self.line1 = plt.plot(weight, weight)
        self.ax1.set_ylim([min(self.chatY), max(self.chatY)*1.2])
        self.line2 = plt.plot(self.chatX1, self.chatY)

        self.line5 = plt.plot([self.zero, self.chatX1[len(self.chatX1) - 4]], [0, self.chatY[len(self.chatY) - 4]],'--o')
        self.line1 = plt.plot([self.zero, self.chatX1[len(self.chatX1) - 3]], [0, self.chatY[len(self.chatY) - 3]],'--o')
        self.line3 = plt.plot([self.zero, self.chatX1[len(self.chatX1) - 2]], [0, self.chatY[len(self.chatY) - 2]],'--o')
        self.line4 = plt.plot([self.zero, self.chatX1[len(self.chatX1) - 1]], [0, self.chatY[len(self.chatY) - 1]],'--o')

        self.ax1.set_xlabel('S')
        self.ax1.set_ylabel('P')

        self.dataAnalysis()

    def traverse_data(self):
        self.line2[0].set_xdata(self.chatX1)
        self.line2[0].set_ydata(self.chatY)

        self.line5[0].set_xdata([self.zero, self.chatX1[len(self.chatX1) - 4]])
        self.line5[0].set_ydata([0, self.chatY[len(self.chatY) - 4]])
        # a1 = plt.text(self.chatX[len(self.chatX) - 4], self.chatY[len(self.chatY) - 4], 'First')

        self.line1[0].set_xdata([self.zero, self.chatX1[len(self.chatX1) - 3]])
        self.line1[0].set_ydata([0, self.chatY[len(self.chatY) - 3]])
        # self.a2 = plt.text(self.chatX[len(self.chatX) - 3], self.chatY[len(self.chatY) - 3], 'First')

        self.line3[0].set_xdata([self.zero, self.chatX1[len(self.chatX1) - 2]])
        self.line3[0].set_ydata([0, self.chatY[len(self.chatY) - 2]])

        # self.a3 = plt.text(self.chatX[len(self.chatX) - 2], self.chatY[len(self.chatY) - 2], 'First')

        self.line4[0].set_xdata([self.zero, self.chatX1[len(self.chatX1) - 1]])
        self.line4[0].set_ydata([0, self.chatY[len(self.chatY) - 1]])

        # self.a4 = plt.text(self.chatX[len(self.chatX) - 1], self.chatY[len(self.chatY) - 1], 'First')
        self.ax1.set_ylim([min(self.chatY), max(self.chatY)*1.2])
        maxX =1
        if max(self.chatX1)*1.6 > 1:
            maxX = max(self.chatX1)*1.6

        self.ax1.set_xlim([self.zero, maxX])

        a1 = self.calculateTheSlope(self.zero, 0, self.chatX1[len(self.chatX1) - 4], self.chatY[len(self.chatY) - 4])
        a2 = self.calculateTheSlope(self.zero, 0, self.chatX1[len(self.chatX1) - 3], self.chatY[len(self.chatY) - 3])
        a3 = self.calculateTheSlope(self.zero, 0, self.chatX1[len(self.chatX1) - 2], self.chatY[len(self.chatY) - 2])
        a4 = self.calculateTheSlope(self.zero, 0, self.chatX1[len(self.chatX1) - 1], self.chatY[len(self.chatY) - 1])
        # print(self.chatX1[len(self.chatX1) - 4])
        # print(self.chatX1[len(self.chatX1) - 3])
        # print(self.chatX1[len(self.chatX1) - 2])
        # print(self.chatX1[len(self.chatX1) - 1])
        # print("---------X---------")
        #
        # print(self.chatY[len(self.chatY) - 4])
        # print(self.chatY[len(self.chatY) - 3])
        # print(self.chatY[len(self.chatY) - 2])
        # print(self.chatY[len(self.chatY) - 1])
        # print("---------Y---------")
        #
        # print(a1)
        # print(a2)
        # print(a3)
        # print(a4)
        # print("---------斜率---------")

        if a1 > a2 > a3 > a4:
            win32api.MessageBox(0, "测试提示", "提示:", win32con.MB_OK)

    # 数据解析
    def dataAnalysis(self):
        self.traverse_data()
        mmap_file = mmap.mmap(0, 1024, access=mmap.ACCESS_WRITE, tagname='share_mmap12')
        while 1:
            mmap_file.seek(0)
            str1 = mmap_file.read().translate(None, b'\x00').decode()
            if str1 != "":
                db_pkl = str1.split(',')
                if len(db_pkl) > 5:
                    # data_0 = self.calibration_1[0] * float(db_pkl[2]) * float(db_pkl[2]) + self.calibration_1[1] * float(db_pkl[2]) + \
                    #          self.calibration_1[2]
                    self.chatY.append(float(db_pkl[2]))
                    self.chatX1.append(float(db_pkl[3]) - float(db_pkl[4]))
                    self.chatX.append(self.chatX[len(self.chatX) - 1] + 1)
                    self.traverse_data()
            plt.pause(self.lineEdit_14)
            time.sleep(self.lineEdit_14)

    # 计算斜率
    def calculateTheSlope(self, x1, y1, x2, y2):
        x = (y2 - y1) / (x2 - x1)
        return x
